﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CreateProject
{
    class Program
    {
        public static string            g_sFilesType        = "";
        public static List<string>      g_lIncludes         = new List<string>();
        public static string            g_sOutput           = "";
        public static List<string>      g_lFileList         = new List<string>();

        static void Main(string[] args)
        {
            for (int i = 0; i < args.Length; ++i)
            {
                //-i : include
                if (args[i].StartsWith("-i"))
                {
                    g_lIncludes.Add(args[i].Substring(2));
                }
                //-o : output
                else if (args[i].StartsWith("-o"))
                {
                    g_sOutput = args[i].Substring(2);
                }
                //-t : file type
                else if (args[i].StartsWith("-t"))
                {
                    if (g_sFilesType == "")
                    {
                        g_sFilesType += "^*\\.(";
                        g_sFilesType += args[i].Substring(2);
                    }
                    else
                    {
                        g_sFilesType += "|";
                        g_sFilesType += args[i].Substring(2);
                    }
                }
            }

            g_sFilesType += ")$";

            if (g_sFilesType == ")$")
            {
                g_sFilesType = "^*$";
            }

            ScanFiles();

            CreateProjectVC2010();
        }

        public static void ScanFiles()
        {
            Queue<string> tmp = new Queue<string>(g_lIncludes.ToArray());

            while (tmp.Count > 0)
            {
                string cur = tmp.Dequeue();

                foreach (string t in System.IO.Directory.GetFiles(cur))
                {
                    if (System.Text.RegularExpressions.Regex.IsMatch(t, g_sFilesType))
                    {
                        g_lFileList.Add(t);
                    }
                }

                foreach (string t in System.IO.Directory.GetDirectories(cur))
                {
                    tmp.Enqueue(t);
                }
            }
        }

        public static string MergePath(string afile1, string afile2, string sep = "\\" )
        {
            string result = "";

            bool check = true;
            string path1 = afile1;
            string path2 = afile2;
            
            while (check)
            {
                check = false;
                int i11 = path1.IndexOf('/');
                int i12 = path1.IndexOf('\\');
                int i21 = path2.IndexOf('/');
                int i22 = path2.IndexOf('\\');

                if (i11 >= 0 && i21 >= 0 && i11 == i21 && string.Compare(path1.Substring(0, i11), path2.Substring(0, i21), true) == 0)
                {
                    path1 = path1.Substring(i11 + 1);
                    path2 = path2.Substring(i21 + 1);
                    check = true;
                }
                else if (i11 >= 0 && i22 >= 0 && i11 == i22 && string.Compare(path1.Substring(0, i11), path2.Substring(0, i22), true) == 0)
                {
                    path1 = path1.Substring(i11 + 1);
                    path2 = path2.Substring(i22 + 1);
                    check = true;
                }
                else if (i12 >= 0 && i22 >= 0 && i12 == i22 && string.Compare(path1.Substring(0, i12), path2.Substring(0, i22), true) == 0)
                {
                    path1 = path1.Substring(i12 + 1);
                    path2 = path2.Substring(i22 + 1);
                    check = true;
                }
                else if (i12 >= 0 && i21 >= 0 && i12 == i21 && string.Compare(path1.Substring(0, i12), path2.Substring(0, i21), true) == 0)
                {
                    path1 = path1.Substring(i12 + 1);
                    path2 = path2.Substring(i21 + 1);
                    check = true;
                }
                else { }
            }

            int count = 0;

            foreach (string t in System.Text.RegularExpressions.Regex.Split(path1, "(/|\\\\)"))
            {
                if (!System.Text.RegularExpressions.Regex.IsMatch(t, "^(/|\\\\)$"))
                {
                    ++count;
                }
                else { }
            }

            for (int i = 0; i < count - 1; ++i)
            {
                result += "..\\";
            }

            result += path2;

            result = System.Text.RegularExpressions.Regex.Replace(result, "(/|\\\\)", sep);

            return result;
        }

        public static void CreateProjectVC2010()
        {
            string sFolderPath = System.IO.Path.Combine(g_sOutput, ConstValues.VC2010_FOLDER_NAME);

            if (!System.IO.Directory.Exists(sFolderPath))
            {
                if (System.IO.File.Exists(sFolderPath))
                {
                    System.IO.File.Delete(sFolderPath);
                }
                else { }
            }
            else
            {
                System.IO.Directory.Delete(sFolderPath);
            }

            System.IO.Directory.CreateDirectory(sFolderPath);

            foreach (string f in g_lFileList)
            {
                if (System.Text.RegularExpressions.Regex.IsMatch(f, ConstValues.VC2010_Group_ClCompile))
                {
                }
                else if (System.Text.RegularExpressions.Regex.IsMatch(f, ConstValues.VC2010_Group_ClInclude))
                {
                }
                else if (System.Text.RegularExpressions.Regex.IsMatch(f, ConstValues.VC2010_Group_Midl))
                {
                }
                else if (System.Text.RegularExpressions.Regex.IsMatch(f, ConstValues.VC2010_Group_None))
                {
                }
                else
                {
                }
            }
        }
    }
}
